Select/Checkbox save_custom/save_options triggers warnings for JSON-only or unidentified fields#383
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
cbravobernal
left a comment
There was a problem hiding this comment.
Verified locally:
- The four new tests reproduce the reported warnings against current trunk (e.g.
Undefined array key "ID"inclass-acf-field-select.php:585) and pass with this fix. - Full PHPUnit suite (2232 tests), PHPStan, and phpcs all green on this branch.
One behavior change worth calling out explicitly for the record: in the checkbox field, the failed-lookup path previously did return false; (dropping the submitted value entirely), and now returns $value. That's an improvement — consistent with the select/radio handling — but it is a behavior change, not just a warning fix.
Also +1 on adding the is_array( $value ) guard before the save_custom loop.
Verification run via Claude Code, reviewed by @cbravobernal.
Summary
When custom option saving is enabled, Select (
save_options) and Checkbox (save_custom) can emit PHP warnings if the runtime field is JSON-only (no DBID) or missing identifiers.Affected areas
includes/fields/class-acf-field-select.php(acf_field_select::update_value)includes/fields/class-acf-field-checkbox.php(acf_field_checkbox::update_value)Problem
Current logic assumes a valid field identifier (
IDorkey) and a successfulacf_get_field()lookup with a persistedID. In JSON/local-field scenarios this may be false, which can lead to warnings (undefined array key / invalid array access) while trying to append custom choices.Steps to reproduce
save_options = 1(or Checkbox withsave_custom = 1).ID).choices.update_value().Expected behavior
No warnings should be emitted. Value should still be returned/saved, and custom-choice persistence should only run when a valid persisted field can be resolved.
Proposed fix
ID->key-> bail).acf_get_field()result is invalid/noID.choicesto an array before appending custom values.Tests
Added regression tests:
tests/php/includes/fields/test-class-acf-field-select.phptest_update_value_save_options_handles_json_field_without_idtest_update_value_save_options_handles_missing_field_identifiertests/php/includes/fields/test-class-acf-field-checkbox.phptest_update_value_save_custom_handles_json_field_without_idtest_update_value_save_custom_handles_missing_field_identifierPatch reference
Commit:
34cfcb7Branch:
fix/select-checkbox-save-options-guards